package com.ai.notices.web.controller.busi;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ai.notices.common.util.DateUtils;
import com.ai.notices.common.util.InetTool;
import com.ai.notices.dao.mysql.model.LoginLog;
import com.ai.notices.service.interfaces.IAuthStaffSV;
import com.ai.notices.service.interfaces.ILoginLogSV;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.notices.common.constants.Constants;
import com.ai.notices.common.util.StringUtil;
import com.ai.notices.dao.mysql.model.AuthStaff;
import com.ai.notices.utils.mongo.MongoSV;
import com.ai.notices.web.system.AppBaseController;
import com.ai.notices.web.system.util.StaffSessionUtil;

import net.sf.json.JSONObject;

/**
 * Created by huangzh on 17/4/14.
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController extends AppBaseController {
    private static final Logger log = Logger.getLogger(LoginController.class);
    //@Autowired
    //private MongoSV mongo;

    @Autowired
    private IAuthStaffSV iAuthStaffSV;

    @Autowired
    private ILoginLogSV iLoginLogSV;
    
    /*
    * 功能描述:进入登录页面
    * 开发人员:huangzh5
    * */
    @RequestMapping()
    public String init() throws Exception{
    	//JSONObject obj = new JSONObject();
    	//obj.put("aaaa", "aaaa");
    	//String id = mongo.insert("abcdefg", obj);
    	//log.info("id======"+id+"key=abcdefg----value=aaaa");
    	//"5943c942976e6e0955196b0";
    	//mongo.findOne(collectionName, condition, field, orderBy)
        return "/login"; //返回页面
    }
    @RequestMapping(value="/check")
    @ResponseBody
    public Map logincheck(@RequestParam("username")String username, @RequestParam("password")String password,@RequestParam("capcode")String capcode) throws Exception{
        Map retMap = new HashMap();
        retMap.put(Constants.RetFlag.FLAG,Constants.RetFlag.ERROR);

        try {
            //校验传入参数是否为空
            if (StringUtil.isBlank(username)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "用户名为空，请输入用户码!");
                return retMap;
            }
            if (StringUtil.isBlank(password)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "密码为空，请输入密码!");
                return retMap;
            }
            if (StringUtil.isBlank(capcode)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "验证码为空，请输入验证码!");
                return retMap;
            }

            //校验验证码是否正确
            String sRandFont = (String) request.getSession().getAttribute("sRandFont");
            if (!capcode.equals(sRandFont)) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "验证码错误，请重新输入!");
                return retMap;
            }

            //根据用户名查询用户记录
            AuthStaff authStaff = new AuthStaff();
            authStaff.setStaffCode(username);
            List<AuthStaff> retList = iAuthStaffSV.getAuthStaff(authStaff);
            if (null == retList || retList.size() <= 0) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "用户名输入错误，请重新输入!");
                return retMap;
            }

            //验证用户是否有效
            authStaff.setStaffPasswd(password);
            AuthStaff staffInfo = retList.get(0);
            if (!Constants.StaffFlag.VALID.equals(staffInfo.getStaffFlag())) {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "无效用户，请重新输入!");
                return retMap;
            }

            if (authStaff.getStaffPasswd().equals(staffInfo.getStaffPasswd())) {
                //保存日志到登录日志信息表
                String ip = InetTool.getHostAddr();
                LoginLog loginLog = new LoginLog();
                loginLog.setStaffId(staffInfo.getId());
                loginLog.setStaffLoginName(staffInfo.getStaffName());
                loginLog.setInTime(DateUtils.getDate(DateUtils.getDateString("yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss"));
                loginLog.setInIp(ip);
                iLoginLogSV.insertLoginLog(loginLog);

                //将用户信息写入session
                StaffSessionUtil.setStaffInfo(request.getSession(), staffInfo);
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.SUCCESS);
                retMap.put(Constants.RetFlag.MSG, "登录成功!");

                return retMap;
            } else {
                retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
                retMap.put(Constants.RetFlag.MSG, "密码不正确，请重新输入!");
                return retMap;
            }
        }catch (Exception e){
            log.error(e.toString());
            retMap.put(Constants.RetFlag.FLAG, Constants.RetFlag.ERROR);
            retMap.put(Constants.RetFlag.MSG, e.toString());
        }

        return retMap;
    }
}
